package com.itheima.edu.info.manager.B_Controller;

import com.itheima.edu.info.manager.C_Service.StudentService;
import com.itheima.edu.info.manager.E_domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentController {
    private Scanner sc = new Scanner(System.in);
    private ArrayList<Student> list;
    private StudentService ss = new StudentService();
    private Student st = new Student();

    public void jieShou() {
        lo:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生  2 修改学生  3 删除学生  4 查看学生  5 退出");
            int a = sc.nextInt();
            switch (a) {
                case 1: {
                    tianjia();
                    break;
                }
                case 2: {
                    xiuGai();
                    break;
                }
                case 3: {
                    shanChu();
                    break;
                }
                case 4: {
                    chaKan();
                    break;
                }
                case 5: {
                    break lo;
                }
            }
        }
    }


    public void tianjia() {
        while (true) {
            Student stu = new Student();
            System.out.println("请输入学号");
            String id = sc.next();
            if (ss.panDuanID(id)) {
                System.out.println("学号重复");
            } else {
                System.out.println("请输入姓名");
                String name = sc.next();
                System.out.println("请输入年龄");
                int age = sc.nextInt();
                stu.setID(id);
                stu.setName(name);
                stu.setAge(age);
                ss.tianjia(stu);
                System.out.println("添加成功");
                return;
            }
        }

    }

    private void xiuGai() {
        if (list == null || list.isEmpty()) {
            System.out.println("并无学生");
            return;
        }
        while (true) {

            System.out.println("请输入要修改的学生的学号");
            String s = sc.next();
            if (ss.panDuanID(s)) {
                System.out.println("请输入修改的姓名");
                String name = sc.next();
                System.out.println("请输入修改的年龄");
                int age = sc.nextInt();
                st.setID(s);
                st.setName(name);
                st.setAge(age);
                ss.xiuGai(st);
                System.out.println("修改成功");
                return;
            } else {
                System.out.println("学号不存在");
                return;
            }
        }

    }

    private void shanChu() {
        if (list == null || list.isEmpty()) {
            System.out.println("并无学生");
            return;
        }
        System.out.println("请输入要删除的学生的学号");
        String s = sc.next();
        if (ss.panDuanID(s)) {
            ss.shanChu(s);
        } else {
            System.out.println("并无此学号");
        }
    }

    private void chaKan() {
        list = ss.chaKan();
        if (list == null || list.isEmpty()) {
            System.out.println("并无学生");
            return;
        } else {
            for (int i = 0; i < list.size(); i++) {
                Student ss = list.get(i);
                System.out.println("学号\t\t姓名\t\t年龄\t\t");
                System.out.println(ss.getID() + "\t\t" + ss.getName() + "\t\t" + ss.getAge());
            }
        }
    }
}
